PowerWEB LiveControls for ASP.NET
Creating Custom Callbacks
Send comments on this topic.



Glossary Item Box

While PowerWEB LiveControls for ASP.NET includes a suite of controls that mimic many intrinsic Microsoft controls but add callback capability, some users may want to implement their own callbacks. The LiveCallback control enables a callback to occur from a browser to a user-specified function on the server. This implementation is often referred to as remote scripting. The LiveCallback function only allows strings as parameters. Using the LiveCallback control requires the implementation of 3 functions:

  1. The function to call on the server, which will reside in the Code-Behind page for the WebForm, such as WebForm1.aspx.cs.
  2. The Javascript function that will initiate the callback by calling a PowerWEB LiveControls client-side API function called pwBeginCallServerFunction().
  3. The Javascript function that will be called when the server-side result returns.

In this example, a server-side function that adds two numbers will be called from the client and return the result.

  1. Add a function to theWebForm1.aspx.cs class that adds two numbers. Make sure the parameters of this function are strings and that the return value is a string.
    public string Add(string num1, string num2)
    {
            int val = System.Convert.ToInt32(num1) + System.Convert.ToInt32(num2);
            return val.ToString();
    }
    
  2. Add a Microsoft Button control and a LiveCallback control to the WebForm.
  3. In the Page_Load event of the WebForm1 Code-Behind page, add an "OnClick" attribute to the Button control that will trigger a Javascript function called doAdd() when the button is pressed. Then return false to prevent a postback.
    Button1.Attributes.Add("OnClick","doAdd();return false;");
    
  4. Add the doAdd() function to the ASPX page representing WebForm1 that calls pwBeginCallServerFunction(). The first parameter of the function is the name of the function to call on the server. In this case, the name of the server function is Add. The second parameter is the Javascript function that will be called when the server function, in this case Add, will return a result. The Javascript return function is called addResult. The third parameter is any user context information, which should remain an empty string. The fourth parameter is whether the function call should be blocking. The remaining parameters are those that should be passed to the server function. Up to nine parameters can be specified as strings. In the example below, two parameters are passed to the Add function.
    <script language='JavaScript'>
    function doAdd()
    {
            pwBeginCallServerFunction("Add", "addResult", "", false, "3", "3");
    }
    </script>
    
  5. Finally, add the Javascript return function to the ASPX page called addResult() so the whole Javascript block will look like the example below. The return data is in the pwRsCallbackResponse object and can be referenced in the "data" property of the object.
    <script language='JavaScript'>
    function doAdd()
    {
            pwBeginCallServerFunction("Add", "addResult", "", false, "3", "3");
    }
    
    function addResult(pwRsCallbackResponse)
    {
            alert(pwRsCallbackResponse.data);
    }
    </script>
    
  6. Now run the web application and when the button is pressed, the alert window will appear with the number "6." There will be no postback to the server when the calculation occurs, but rather a callback.
Documentation Version 4.0.2
© 2012 Dart Communications. All Rights Reserved.